from common.baseApi import BaseAPI
from configs.config import PARENT_ID
from libs.api.login import LoginAPI


class OrganizAPI(BaseAPI):
    """
    1添加方法不一样 要重写
        添加到根部门id
        获取部门id  通过查询部门的方法获取到id
    2修改 更新方法也要重写
    3.删除所有部门信息的时候重写  保留根部门
    """
    def __init__(self,cookies):
        super(OrganizAPI,self).__init__(cookies)
        # print(self.data)
        orgList = self.query()
        self.org_parentid = orgList[0]['_id']
    def add(self,**kwargs):
        """
        跟部门添加数据添加进去

        :return: 调用父类的方法 把修改过后的数据传到父类方法里
        """
        # parentid = {'parent':PARENT_ID} # 根部门id，在配置文件中保存？要怎么获取最新的？
        organiz_data ={
                        "$select": "name,sort_no,hidden,owner,company_id,company_ids,locked,parent,children",
                        "$filter": "(parent eq null)"
                    }
        # parentid = self.request_send(method='get',params=organiz_data).json()['value'][0]['_id'] # 使用查询方法，得到根部门id
        # parentid = self.query()[0]['_id']
        parentid = {'parent':self.org_parentid}
        kwargs.update(parentid)
        return super(OrganizAPI,self).add(**kwargs)

    def update(self,_id,**kwargs):
        """
        跟部门添加数据添加进去

        :return: 调用父类的方法 把修改过后的数据传到父类方法里
        """
        return super().update(_id=_id,**kwargs)
    # def delete_all_items(self):
    #     """
    #     保留跟部门   查询的时候结果少第一个 列表的切片过滤总部门[1:]
    #     :return:
    #     """
    #     # organiz_data = {
    #     #     "$select": "name,sort_no,hidden,owner,company_id,company_ids,locked,parent,children",
    #     #     "$filter": "(parent eq null)"
    #     # }
    #     return super().delete_all_items()



"""
别的业务类只需要继承基类即可
"""

if __name__ == '__main__':
    cookies = LoginAPI().login('1184882444@qq.com','123456a')
    organz = OrganizAPI(cookies=cookies)
    add_data = { "name": "abcde3", "sort_no": 101,"hidden": 'true', "space": organz.space_id }
    res = organz.add(**add_data)
    # print(res)
    update_data = {"name": "abcde2","sort_no": 101,"parent": "tqrMFDHYrJh6vKczs","hidden": 'true',"space": organz.space_id}
    # update_res = organz.update(_id='CARntAh92FTooahah',**update_data)
    # print(update_res)
    delete_data = {
                        "$select": "name,sort_no,hidden,owner,company_id,company_ids,locked,parent,children",
                        "$filter": "(parent eq null) or (parent eq 'tqrMFDHYrJh6vKczs')"
                    }
    # delete_res = organz.delete(_id='CARntAh92FTooahah')
    # deleteall_res = organz.delete_all_items()
